package com.egao.customer.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.customer.biz.CustomerBiz;
import com.egao.customer.entity.Customer;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-13 13:36:04
 * @version ：1.0
 */
@Repository
public class CustomerBizImpl extends BaseDao implements CustomerBiz {

	@Override
	public JSONObject list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.card,c.name");
        sql.append(",c.id_card,c.phone,c.pic,c.address,c.price,c.pay_time");
        sql.append(",c.commercial,c.commercial_unuse,c.civil,c.civil_unuse,c.violations");
        sql.append(",c.advance_charge,c.advance_charge_time,c.advance_charge_discount,c.advance_charge_remark");
		sql.append(",c.heat_area,c.total_area,c.state,c.tb_state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from sf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getCard())){
				sql.append(" and instr(c.card,:card)>0 ");
				map.put("card", customer.getCard());
			}
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
			if(Utils.isNotEmpty(customer.getIdCard())){
				sql.append(" and instr(c.id_card,:id_card)>0 ");
				map.put("id_card", customer.getIdCard());
			}
			if(Utils.isNotEmpty(customer.getPhone())){
				sql.append(" and instr(c.phone,:phone)>0 ");
				map.put("phone", customer.getPhone());
			}
			if(Utils.isNotEmpty(customer.getState())){
				sql.append(" and instr(c.state,:state)>0 ");
				map.put("state", customer.getState());
			}
			if(Utils.isNotEmpty(customer.getTbState())){
				sql.append(" and c.tb_state=:tb_state ");
				map.put("tb_state", customer.getTbState());
			}
			if(Utils.isNotEmpty(customer.getPayTime())){
				if(customer.getPayTime().equals("已缴费")){
					sql.append(" and c.pay_time>=DATE_FORMAT(now(),'%Y') ");
				}
				if(customer.getPayTime().equals("未缴费")){
					sql.append(" and (c.pay_time<DATE_FORMAT(now(),'%Y') or c.pay_time is null) ");
				}
			}

			if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
				sql.append(" order by "+customer.getSort()+" "+customer.getOrder());
			}
		}
		return jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Customer> list(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.card,c.name");
        sql.append(",c.id_card,c.phone,c.pic,c.address,c.price,c.pay_time");
		sql.append(",c.commercial,c.commercial_unuse,c.civil,c.civil_unuse,c.violations");
		sql.append(",c.advance_charge,c.advance_charge_time,c.advance_charge_discount,c.advance_charge_remark");
		sql.append(",c.heat_area,c.total_area,c.state,c.tb_state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from sf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getCard())){
				sql.append(" and instr(c.card,:card)>0 ");
				map.put("card", customer.getCard());
			}
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
			if(Utils.isNotEmpty(customer.getIdCard())){
				sql.append(" and instr(c.id_card,:id_card)>0 ");
				map.put("id_card", customer.getIdCard());
			}
			if(Utils.isNotEmpty(customer.getPhone())){
				sql.append(" and instr(c.phone,:phone)>0 ");
				map.put("phone", customer.getPhone());
			}
			if(Utils.isNotEmpty(customer.getState())){
				sql.append(" and instr(c.state,:state)>0 ");
				map.put("state", customer.getState());
			}
			if(Utils.isNotEmpty(customer.getTbState())){
				sql.append(" and c.tb_state=:tb_state ");
				map.put("tb_state", customer.getTbState());
			}
			if(Utils.isNotEmpty(customer.getPayTime())){
				if(customer.getPayTime().equals("已缴费")){
					sql.append(" and c.pay_time>=DATE_FORMAT(now(),'%Y') ");
				}
				if(customer.getPayTime().equals("未缴费")){
					sql.append(" and (c.pay_time<DATE_FORMAT(now(),'%Y') or c.pay_time is null) ");
				}
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Customer getById(Customer customer) {
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.card,c.name");
        sql.append(",c.id_card,c.phone,c.pic,c.address,c.price,c.pay_time");
		sql.append(",c.commercial,c.commercial_unuse,c.civil,c.civil_unuse,c.violations");
		sql.append(",c.advance_charge,c.advance_charge_time,c.advance_charge_discount,c.advance_charge_remark");
		sql.append(",c.heat_area,c.total_area,c.state,c.tb_state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from sf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 and c.id=:id ");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public Customer getByCard(Customer customer) {
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.card,c.name");
        sql.append(",c.id_card,c.phone,c.pic,c.address,c.price,c.pay_time");
		sql.append(",c.commercial,c.commercial_unuse,c.civil,c.civil_unuse,c.violations");
		sql.append(",c.advance_charge,c.advance_charge_time,c.advance_charge_discount,c.advance_charge_remark");
		sql.append(",c.heat_area,c.total_area,c.state,c.tb_state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from sf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 and c.card=:card ");
		map.put("card", customer.getCard());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sf_customer (");
        sql.append("company_id,card,name,id_card,phone");
		sql.append(",commercial,commercial_unuse,civil,civil_unuse,violations");
		sql.append(",advance_charge,advance_charge_time,advance_charge_discount,advance_charge_remark");
		sql.append(",pic,address,price,heat_area,total_area,state,tb_state,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":company_id,:card,:name,:id_card,:phone");
		sql.append(",:commercial,:commercial_unuse,:civil,:civil_unuse,:violations");
		sql.append(",:advance_charge,:advance_charge_time,:advance_charge_discount,:advance_charge_remark");
		sql.append(",:pic,:address,:price,:heat_area,:total_area,:state,:tb_state,:remark");
		sql.append(",:create_user,now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("card", customer.getCard());
        map.put("name", customer.getName());
        map.put("id_card", customer.getIdCard());
        map.put("phone", customer.getPhone());
        map.put("commercial", customer.getCommercial());
        map.put("commercial_unuse", customer.getCommercialUnuse());
        map.put("civil", customer.getCivil());
        map.put("civil_unuse", customer.getCivilUnuse());
        map.put("violations", customer.getViolations());
        map.put("advance_charge", customer.getAdvanceCharge());
        map.put("advance_charge_time", customer.getAdvanceChargeTime());
        map.put("advance_charge_discount", customer.getAdvanceChargeDiscount());
        map.put("advance_charge_remark", customer.getAdvanceChargeRemark());
        map.put("pic", customer.getPic());
        map.put("address", customer.getAddress());
        map.put("price", customer.getPrice());
        map.put("heat_area", customer.getHeatArea());
        map.put("total_area", customer.getTotalArea());
        map.put("state", customer.getState());
        map.put("tb_state", customer.getTbState());
        map.put("remark", customer.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("name=:name,id_card=:id_card,phone=:phone ");
		sql.append(",advance_charge_discount=:advance_charge_discount");
		sql.append(",pic=:pic,address=:address,price=:price,heat_area=:heat_area,total_area=:total_area ");
		sql.append(",commercial=:commercial,commercial_unuse=:commercial_unuse,civil=:civil,civil_unuse=:civil_unuse,violations=:violations ");
		sql.append(",state=:state,remark=:remark,pay_time=:pay_time" +
		",update_user=:update_user,update_time=now() where id=:id ");
		map.put("name", customer.getName());
		map.put("id_card", customer.getIdCard());
		map.put("phone", customer.getPhone());
		map.put("advance_charge_discount", customer.getAdvanceChargeDiscount());
		map.put("commercial", customer.getCommercial());
		map.put("commercial_unuse", customer.getCommercialUnuse());
		map.put("civil", customer.getCivil());
		map.put("civil_unuse", customer.getCivilUnuse());
		map.put("violations", customer.getViolations());
		map.put("pic", customer.getPic());
		map.put("address", customer.getAddress());
		map.put("price", customer.getPrice());
		map.put("heat_area", customer.getHeatArea());
		map.put("total_area", customer.getTotalArea());
		map.put("state", customer.getState());
		map.put("remark", customer.getRemark());
		map.put("pay_time", customer.getPayTime());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean violations(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("violations=:violations ");
		sql.append(",state=:state" +
				",update_user=:update_user,update_time=now() where id=:id ");
		map.put("violations", customer.getViolations());

		map.put("state", customer.getState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
		public boolean advanceChargeDiscount(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("advance_charge_discount=:advance_charge_discount");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
		map.put("advance_charge_discount", customer.getAdvanceChargeDiscount());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

//	@Override
//		public boolean advanceCharge(Customer customer, User loginUser) {
//		Map<String, String> map  = new HashMap<String, String>();
//		StringBuffer sql =new StringBuffer("update sf_customer set ");
//		sql.append(",advance_charge=:advance_charge,advance_charge_time=:advance_charge_time,advance_charge_discount=:advance_charge_discount");
//		sql.append(",state=:state" +
//				",update_user=:update_user,update_time=now() where id=:id ");
//		map.put("advance_charge", customer.getAdvanceCharge());
//		map.put("advance_charge_time", customer.getAdvanceChargeTime());
//		map.put("advance_charge_discount", customer.getAdvanceChargeDiscount());
//		map.put("update_user", loginUser.getUserId() + "");
//		map.put("id", customer.getId());
//		getMapParameter(sql, map);
//		int count = jdbcTemplate.update(sql.toString(), map);
//		return count > 0;
//	}

	@Override
	public boolean updateState(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("state=:state");
		sql.append(",update_user=:update_user,update_time=DATE_FORMAT(now(),'%Y-%m-%d') where id=:id ");
        map.put("state", customer.getState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean cardUpdate(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("card=:card");
//		sql.append("pay_time=DATE_FORMAT(now(),'%Y')");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("card", customer.getCard());
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean jf(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("pay_time=:pay_time");
//		sql.append("pay_time=DATE_FORMAT(now(),'%Y')");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("pay_time", customer.getPayTime());
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateYfk(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("advance_charge=advance_charge-:advance_charge");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("advance_charge", customer.getAdvanceCharge());
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
	@Override
	public boolean yfk(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("advance_charge=advance_charge+:advance_charge");
		sql.append(",advance_charge_time=:advance_charge_time");
		sql.append(",advance_charge_remark=:advance_charge_remark");
//		sql.append("pay_time=DATE_FORMAT(now(),'%Y')");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("advance_charge", customer.getAdvanceCharge());
		map.put("advance_charge_time", customer.getAdvanceChargeTime());
		map.put("advance_charge_remark", customer.getAdvanceChargeRemark());
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateTbState(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sf_customer set ");
		sql.append("tb_state=:tb_state");
		sql.append(",update_user=:update_user,update_time=now() where id=:id ");
        map.put("tb_state", customer.getTbState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Customer customer, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(customer.getId().length()>0){
			for(String ID : customer.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sf_customer set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
